/**
 * Created by 叶子 on 2017/7/30.
 */
import { combineReducers } from 'redux';
import * as type from '../action/type';

const handleData = (state = {isFetching: true, data: {}}, action) => {
    switch (action.type) {
        case type.REQUEST_DATA:
            return {...state, isFetching: true};
        case type.RECEIVE_DATA:
            return {...state, isFetching: false, data: action.data};
        default:
            return {...state};
    }
};
const httpData = (state = {}, action) => {
    switch (action.type) {
        case type.RECEIVE_DATA:
        case type.REQUEST_DATA:
            return {
                ...state,
                [action.category]: handleData(state[action.category], action)
            };
        default:
            return {...state};
    }
};


/**
 * author heshengjin
 * date 2017-10-10
 * 将counter加上1
 */
const countData = (state = {}, action) => {
    switch (action.type) {
        case type.COUNT_DATA:
            let nState = state[action.category] ? (action.data + state[action.category]) : (action.data);
            return {
                ...state,
                [action.category]:  nState
            };
        default:
            return {...state};
    }
};



/**
 * @author heshengjin
 * @date 2017-10-14
 * 直接在reduce里面初始化，对比COUNT_DATA在获取的时候初始化
 * 不要category
 * @type {string}
 */
const countDataInit = (state = {counterinit:0}, action) => {
    switch (action.type) {
        case type.COUNT_DATA_INIT:
            return {
                ...state,
                counterinit:  state.counterinit + action.data
            };
        default:
            return {...state};
    }
};

/**
 * author heshengjin
 * date 2017-10-10
 * 增加导出countData
 */
export default combineReducers({
    httpData,
    countData,
    countDataInit
});
